<?php
class InsuranceInfo {
	// Insurance type constants
	const  TYPE_DN  =   'DN';
	const  TYPE_HX  =   'HX';
	const  TYPE_CH  =   'CH';
	const  TYPE_NN  =   'NN';
	const  TYPE_TK  =   'TK';
	const  TYPE_HC  =   'HC';
	const  TYPE_XK  =   'XK';
	const  TYPE_CA  =   'CA';
	const  TYPE_HT  =   'HT';
	const  TYPE_TB  =   'MS';
	const  TYPE_XB  =   'XB';
	const  TYPE_XN  =   'XN';
	const  TYPE_TN  =   'TN';
	const  TYPE_CC  =   'CC';
	const  TYPE_CK  =   'CK';
	const  TYPE_CB  =   'CB';
	const  TYPE_KC  =   'KC';
	const  TYPE_HD  =   'HD';
	const  TYPE_BT  =   'BT';
	const  TYPE_HN  =   'HN';
	const  TYPE_TC  =   'TC';
	const  TYPE_TQ  =   'TQ';
	const  TYPE_TA  =   'TA';
	const  TYPE_TY  =   'TY';
	const  TYPE_TE  =   'TE';
	const  TYPE_HG  =   'HG';
	const  TYPE_LS  =   'LS';
	const  TYPE_CN  =   'CN';
	const  TYPE_HS  =   'HS';
	const  TYPE_GD  =   'GD';
	const  TYPE_TL  =   'TL';
	const  TYPE_XV  =   'XV';
	const  TYPE_NO  =   'NO';

	// Privete variables
	private $insuranceNumber;
	private $type = false;
	private $paidFor36Month = -1;
	private $correctInsuranceRoute = 0;
	private $date_issued;
	private $date_expired;
	private $registered_hospital   =   '';
    private $insurance_id           =   '';

	function __construct($insuranceId = '', $insuranceNumber = '', $paidFor36Month = -1, $correctRoute = 0, $dateIssued = '', $dateExpired = '',
	           $registerdHospital = '') {
        $this->insurance_id =   $insuranceId;
        $this->insuranceNumber    =   $insuranceNumber;
		$this->type      =    self::parse($insuranceNumber);
		$this->paidFor36Month =   $paidFor36Month;
		$this->correctInsuranceRoute  =   $correctRoute;
		$this->date_issued    =   $dateIssued;
		$this->date_expired   =   $dateExpired;
		$this->registered_hospital    =   $registerdHospital;
	}

	/**#@+
     * Getters
     */
	public function getPaidFor36Month() {
		return $this->paidFor36Month;
	}

	public function isCorrectInsuranceRoute() {
		return $this->correctInsuranceRoute;
	}

	public function getInsuranceGroup() {
		return $this->type;
	}

    /**#@-*/

    /**#@+
     * Setters
     */
	public function setPaidFor36Month($val) {
		$this->paidFor36Month =   $val;
	}

	public function setCorrectInsuranceRoute($val) {
		$this->correctInsuranceRoute  =   $val;
	}
	/**#@-*/

    /**
     * Return insurance onject code of given insurance number.
     *
     * @param mixed $insuranceNumber Insurance number
     * @return An insurance object code or false if the inusurance number invalid
     */
	private static function parse($insuranceNumber){
        $pattern  =   '/^(\w{2})-(\d{1})-(\d{2})-(\d{2})-(\d{3})-(\d{5})$/';
        if (!preg_match($pattern, $insuranceNumber, $matches)) {
            return false;
        }

        // Validate insurance object code
        $objectCode =   $matches[1];
        $codeList   =   self::getCodeList();
        if (!in_array($objectCode, $codeList)) {
            return false;
        }

        // Validate benefit code
        $benefitCode    =   (int)$matches[2];
        if ($benefitCode < 1 || $benefitCode > 9) {
            return false;
        }

        // TODO ppdong: check province code

        return $objectCode;
	}

	private static function getCodeList() {
        // Get the insurance object code list
        $codeList =   array('DN','HX','CH', 'NN', 'TK', 'HC', 'XK', 'CA', 'HT', 'TB', 'MS',
            'XB', 'XN', 'TN', 'CC', 'CK', 'CB', 'KC', 'HD', 'BT', 'HN', 'TC', 'TQ', 'TA', 'TY',
            'TE', 'HG', 'LS', 'CN', 'HS', 'GD', 'TL', 'XV', 'NO');
        return $codeList;
	}

	public function getInsuranceNumber() {

		return $this->insuranceNumber;
	}

	public function getIssuedDate($localized = false) {
        if ($localized) {
            return L10nHelper::renderDateTime($this->date_issued);
        }
		return $this->date_issued;
	}

    public function getExpiredDate($localized = false) {
        if ($localized) {
            return L10nHelper::renderDateTime($this->date_expired);
        }
        return $this->date_expired;
    }

    public function getRegisteredHospital() {
    	return $this->registered_hospital;
    }

    public function getInsuranceId() {
    	return $this->insurance_id;
    }
}
?>